<script setup>
import { Background } from '@vue-flow/background'
import { Panel, VueFlow, useVueFlow } from '@vue-flow/core'
import { nextTick, ref } from 'vue'
import AnimationEdge from './AnimationEdge.vue'
import Icon from './Icon.vue'
import ProcessNode from './ProcessNode.vue'

import { initialEdges, initialNodes } from './initial-elements.js'
import { useLayout } from './useLayout'
import { useRunProcess } from './useRunProcess'
import { useShuffle } from './useShuffle'

const nodes = ref(initialNodes)

const edges = ref(initialEdges)

const cancelOnError = ref(true)

const shuffle = useShuffle()

const { graph, layout, previousDirection } = useLayout()

const { run, stop, reset, isRunning } = useRunProcess({ graph, cancelOnError })

const { fitView } = useVueFlow()

async function shuffleGraph() {
  await stop()

  reset(nodes.value)

  edges.value = shuffle(nodes.value)

  nextTick(() => {
    layoutGraph(previousDirection.value)
  })
}

async function layoutGraph(direction) {
  await stop()

  reset(nodes.value)

  nodes.value = layout(nodes.value, edges.value, direction)

  nextTick(() => {
    fitView()
  })
}
</script>

<template>
  <div class="layout-flow">
    <VueFlow :nodes="nodes" :edges="edges" @nodes-initialized="layoutGraph('LR')">
      <template #node-process="props">
        <ProcessNode :data="props.data" :source-position="props.sourcePosition" :target-position="props.targetPosition" />
      </template>

      <template #edge-animation="edgeProps">
        <AnimationEdge
          :id="edgeProps.id"
          :source="edgeProps.source"
          :target="edgeProps.target"
          :source-x="edgeProps.sourceX"
          :source-y="edgeProps.sourceY"
          :targetX="edgeProps.targetX"
          :targetY="edgeProps.targetY"
          :source-position="edgeProps.sourcePosition"
          :target-position="edgeProps.targetPosition"
        />
      </template>

      <Background />

      <Panel class="process-panel" position="top-right">
        <div class="layout-panel">
          <button v-if="isRunning" class="stop-btn" title="stop" @click="stop">
            <Icon name="stop" />
            <span class="spinner" />
          </button>
          <button v-else title="start" @click="run(nodes)">
            <Icon name="play" />
          </button>

          <button title="set horizontal layout" @click="layoutGraph('LR')">
            <Icon name="horizontal" />
          </button>

          <button title="set vertical layout" @click="layoutGraph('TB')">
            <Icon name="vertical" />
          </button>

          <button title="shuffle graph" @click="shuffleGraph">
            <Icon name="shuffle" />
          </button>
        </div>

        <div class="checkbox-panel">
          <label>Cancel on error</label>
          <input v-model="cancelOnError" type="checkbox" />
        </div>
      </Panel>
    </VueFlow>
  </div>
</template>


<style scoped>
@import "./main.css";
</style>


<style>
.layout-flow {
  background-color: #1a192b;
  block-size: 100%;
  inline-size: 100%;
}

.process-panel,
.layout-panel {
  display: flex;
  gap: 10px;
}

.process-panel {
  display: flex;
  flex-direction: column;
  padding: 10px;
  border-radius: 8px;
  background-color: #2d3748;
  box-shadow: 0 0 10px rgba(0, 0, 0, 50%);
}

.process-panel button {
  border: none;
  border-radius: 8px;
  background-color: #4a5568;
  box-shadow: 0 0 10px rgba(0, 0, 0, 50%);
  color: white;
  cursor: pointer;
}

.process-panel button {
  display: flex;
  align-items: center;
  justify-content: center;
  block-size: 40px;
  font-size: 16px;
  inline-size: 40px;
}

.checkbox-panel {
  display: flex;
  align-items: center;
  gap: 10px;
}

.process-panel button:hover,
.layout-panel button:hover {
  background-color: #2563eb;
  transition: background-color 0.2s;
}

.process-panel label {
  color: white;
  font-size: 12px;
}

.stop-btn svg {
  display: none;
}

.stop-btn:hover svg {
  display: block;
}

.stop-btn:hover .spinner {
  display: none;
}

.spinner {
  border: 3px solid #f3f3f3;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  block-size: 10px;
  border-block-start: 3px solid #2563eb;
  inline-size: 10px;
}

@keyframes spin {
  0% {
    transform: rotate(0deg);
  }

  100% {
    transform: rotate(360deg);
  }
}
</style>
